/**
 * 
 */
package com.product.profile.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.product.profile.model.Profile;

/**
 * @author asoundar
 *
 */
@Repository
public class ProfileDAOImpl implements ProfileDAO {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void addProfile(Profile profile) {
		getCurrentSession().save(profile);
	}

	@Override
	public void updateProfile(Profile profile) {
		getCurrentSession().update(profile);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profile> getProfiles() {
		return getCurrentSession().createQuery("from Profile").list();
	}

	@Override
	public void deleteProfile(Long profileId) {
		Profile profile = (Profile) sessionFactory.getCurrentSession().load(
				Profile.class, profileId);
		if (null != profile) {
			getCurrentSession().delete(profile);
		}
	}

	@Override
	public Profile getProfile(String profileId) {
		return (Profile) sessionFactory.getCurrentSession().load(Profile.class,
				profileId);
	}

}
